import json


class config:

    def __init__(self):
        self.value = None

    def load(self):
        if self.value == None:
            with open('config.json', 'r', encoding='utf-8') as configFile:
                self.value = json.loads(configFile.read())
        return self.value

    # def getExtent(self):
    #     configData = self.load()
    #     return configData['extent']

    def getFirstExtent(self):
        configData = self.load()
        return configData['extent'][0]

    def getNextExtent(self,extent):
        configData = self.load()
        extents = configData['extent']
        extentLen = len(extents)
        extentIdx = extents.index(extent)
        if extentIdx + 1 >= extentLen:
            return None
        return extents[extentIdx + 1]

    def getFirstTag(self):
        configData = self.load()
        return configData['tags'][0]

    def getNextTag(self, tag):
        configData = self.load()
        tags = configData['tags']
        tagLen = len(tags)
        tagIdx = tags.index(tag)
        if tagIdx + 1 >= tagLen:
            return None
        return tags[tagIdx + 1]

    def getFirstAk(self):
        configData = self.load()
        return configData['aks'][0]

    def getNextAk(self,ak):
        configData = self.load()
        aks = configData['aks']
        akLen = len(aks)
        akIdx = aks.index(ak)
        if akIdx + 1 >= akLen:
            return None
        return aks[akIdx + 1]


class fetchRecord:

    def __init__(self, queryType='', region='', page_num=0, ak='', extent=None, cur_extent=None):
        self.queryType = queryType
        self.region = region
        self.page_num = page_num
        self.ak = ak
        self.extent = extent
        self.cur_extent = cur_extent

    def save(self):
        with open('record.txt', 'w', encoding='utf-8') as f:
            f.write(str({
                    "queryType": self.queryType,
                    #"region": self.region,
                    "page_num": self.page_num,
                    "ak": self.ak,
                    "extent": self.extent,
                    "cur_extent": self.cur_extent
                    }))

    def load(self):
        c = config()
        with open('record.txt', 'r', encoding='utf-8') as f:
            recordData = json.loads(json.dumps(eval(f.read())))
            self.queryType = recordData['queryType'] if recordData['queryType'] != '' else c.getFirstTag()
            #self.region = recordData['region'] if recordData['region'] != '' else c.getFirstCity()
            self.page_num = recordData['page_num']
            self.ak = recordData['ak'] if recordData['ak'] != '' else c.getFirstAk()
            self.extent = recordData['extent'] if recordData['extent'] != '' else c.getFirstExtent()
            self.cur_extent = recordData['cur_extent']
        return self

class errorData:

    def __init__(self, data):
        self.data = data

    def save(self):
        with open('errorData.txt', 'a', encoding='utf-8',) as f:
            f.write(str(self.data) + '\n')

class timeoutRequest:

    def __init__(self, queryType=None, page_num=0, ak=None, extent=None):
        self.queryType = queryType
        self.page_num = page_num
        self.ak = ak
        self.extent = extent
    
    def save(self):
        with open('timeoutRequest.txt', 'a', encoding='utf-8') as f:
            f.write(str({
                    "queryType": self.queryType,
                    "page_num": self.page_num,
                    "ak": self.ak,
                    "extent": self.extent
                    }) + '\n')
                
    def load(self):
        arr = []
        with open('timeoutRequest.txt', 'r', encoding='utf-8') as f:
            lines = f.readlines()
            for line in lines:
                arr.append(json.loads(json.dumps(eval(line))))
        return arr